﻿using HardcoreGame.Content.Items.Placeable.Banners;
using Terraria;
using Terraria.GameContent.Bestiary;
using Terraria.ID;
using Terraria.ModLoader;

namespace HardcoreGame.Content.NPCs.Enemies.Events.GeomagneticStorm
{
    public class StaticSpiderGround : ModNPC
    {
        public override void SetDefaults()
        {
            NPC.lifeMax = 50;
            NPC.damage = 12;
            NPC.defense = 6;
            NPC.knockBackResist = 0f;
            NPC.width = 34;
            NPC.height = 36;
            NPC.aiStyle = 1;
            NPC.npcSlots = 0.5f;
            NPC.HitSound = SoundID.NPCHit1;
            NPC.noGravity = false;
            NPC.buffImmune[20] = true;
            NPC.buffImmune[70] = true;
            NPC.npcSlots = 2f;
            NPC.DeathSound = SoundID.NPCDeath1;
            NPC.value = Item.buyPrice(0, 0, 1, 15);
            AnimationType = 1;
            NPC.rarity = 1;
            SpawnModBiomes = [ModContent.GetInstance<Biomes.GeomagneticStorm>().Type];
            Banner = NPC.type;
            BannerItem = ModContent.ItemType<StaticSpiderBanner>();
        }
        public override void OnHitPlayer(Player target, Player.HurtInfo hurtInfo)
        {
            target.AddBuff(BuffID.Electrified, 360);
        }
        public override void SetStaticDefaults()
        {
            NPCID.Sets.NPCBestiaryDrawModifiers value = new()
            {
                Hide = true
            };
            NPCID.Sets.NPCBestiaryDrawOffset.Add(Type, value);
            Main.npcFrameCount[NPC.type] = 2;
        }

        public override void ApplyDifficultyAndPlayerScaling(int numPlayers, float balance, float bossAdjustment)
        {
            NPC.lifeMax = NPC.lifeMax;
            NPC.damage = NPC.damage;
        }
        public override void AI()
        {
            NPC.TargetClosest(true);
            Player player = Main.player[NPC.target];
            if (player.position.X > (double)NPC.position.X)
            {
                NPC.spriteDirection = 1;
            }
            else if (player.position.X < (double)NPC.position.X)
            {
                NPC.spriteDirection = -1;
            }
            int num = (int)NPC.Center.X / 16;
            int num2 = (int)NPC.Center.Y / 16;
            bool flag = false;
            for (int i = num - 1; i <= num + 1; i++)
            {
                for (int j = num2 - 1; j <= num2 + 1; j++)
                {
                    if (Main.tile[i, j] == null)
                    {
                        return;
                    }
                    if (Main.tile[i, j].WallType > 0)
                    {
                        flag = true;
                    }
                }
            }
            if (!flag)
            {
                return;
            }
            NPC.Transform(ModContent.NPCType<StaticSpider>());
        }      
        public override void HitEffect(NPC.HitInfo hit)
        {
            if (NPC.life <= 0)
            {
                for (int i = 0; i < 20; i++)
                {
                    Dust.NewDust(NPC.position, NPC.width, NPC.height, 226, 2.5f * hit.HitDirection, -2.5f, 0, default, 1f);
                }
                return;
            }
            int num = 0;
            while (num < hit.Damage / (double)NPC.lifeMax * 50.0)
            {
                Dust.NewDust(NPC.position, NPC.width, NPC.height, 226, hit.HitDirection, -1f, 0, default, 0.8f);
                num++;
            }
        }
    }
}
